import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:material_color_utilities/material_color_utilities.dart';
import 'package:smart_shoes/define/define_colors.dart';
import 'package:smart_shoes/tools/String_tool.dart';
import 'package:smart_shoes/tools/screen_tool.dart';
import 'package:smart_shoes/tools/style_tools.dart';
import 'package:smart_shoes/tools/widget_tools.dart';

///添加设备
class AddDevicePage extends StatefulWidget {
  const AddDevicePage({super.key});

  @override
  State<AddDevicePage> createState() => _AddDevicePageState();
}

class _AddDevicePageState extends State<AddDevicePage> {
  TimeOfDay? _selectedTime;

  // 下拉菜单选项
  final List<String> _deviceTypes = ['类型1', '类型2', '类型3'];
  // 当前选中的选项
  String? _selectedDeviceType;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("添加设备")),
      body: SingleChildScrollView(
        child: GestureDetector(
          onTap: () {
            // 让当前焦点范围失去焦点，从而隐藏键盘
            FocusScope.of(context).unfocus();
          },
          child: Column(
            children: [
              _textFieldItem("设备名称:", "请输入设备名称，最多6位字符"),
              _textFieldItem("设备编号:", "请输入10位数编号"),
              _dropdownItem("所在城市:", "选择所在城市"),
              _textFieldItem("学校名称:", "请输入学校名称"),
              _timePickerItem("在校时间:", isReveal: true),
              _timePickerItem("", isReveal: true),

              _textFieldItem("学生姓名:", "请输入学生姓名"),
              _textFieldItem("学生体重:", "请输入学生体重", unit: "kg"),
              _textFieldItem("学生身高:", "请输入学生身高", unit: "cm"),
              _option(),
            ],
          ),
        ),
      ),
    );
  }

  Widget _option() {
    return Container(
      padding: EdgeInsets.only(left: 12, right: 12),
      child: Row(
        children: [
          WidgetTools().createCustomInkWellButton(
            "取消添加",
            () {
              // 隐藏键盘
              FocusScope.of(context).unfocus();
              // 返回上一个页面
              Get.back();
            },
            textColor: CustomColors.textColor,
            buttonWidth: 170,
            height: 40,
            radius: 4,
            borderColor: CustomColors.uncheckedColor,
            borderWidth: 1,
            shadow: BoxShadow(),
            fontSize: 12,
            bgColor: Colors.white,
          ),
          Expanded(child: SizedBox(width: 10)),
          WidgetTools().createCustomInkWellButton(
            "确定",
            () {},
            textColor: CustomColors.endColor,
            buttonWidth: 170,
            height: 40,
            radius: 4,
            borderColor: CustomColors.uncheckedColor,
            borderWidth: 1,
            shadow: BoxShadow(),
            fontSize: 12,
            bgColor: CustomColors.buttonColor,
          ),
        ],
      ),
    );
  }

  /// 构建输入框边框样式，设置为无边框
  InputBorder buildInputBorder() {
    return InputBorder.none;
  }

  // 显示时间选择对话框
  Future<void> _selectTime() async {
    final TimeOfDay? picked = await showTimePicker(
      context: context,
      initialTime: _selectedTime ?? TimeOfDay.now(),
    );
    if (picked != null) {
      setState(() {
        _selectedTime = picked;
      });
    }
  }

  Widget _timePickerItem(String title, {bool isReveal = false}) {
    return _basicsItem(
      isReveal: isReveal,
      title,
      InkWell(
        onTap: _selectTime,
        child: Container(
          alignment: Alignment.centerLeft,
          child: Text(
            _selectedTime != null
                ? StringTool.formatTimeOfDay(_selectedTime!)
                : '00:00:00',
            style: TextStyle(
              color:
                  _selectedTime != null
                      ? CustomColors.textColor
                      : CustomColors.greyColor,
            ),
          ),
        ),
      ),
    );
  }

  Widget _dropdownItem(String title, String hintText) {
    return _basicsItem(
      title,
      Container(
        child: DropdownButton(
          isExpanded: true,
          value: _selectedDeviceType,
          alignment: Alignment.center,
          items:
              _deviceTypes.map((String value) {
                return DropdownMenuItem<String>(
                  value: value,
                  child: Text(value),
                );
              }).toList(),
          hint: Text(hintText),
          onChanged: (newValue) {
            setState(() {
              _selectedDeviceType = newValue;
            });
          },
          underline: Container(),
        ),
      ),
    );
  }

  Widget _basicsItem(
    String title,
    Widget subView, {
    bool isReveal = false,
    String unit = "",
  }) {
    Widget icon = Text(unit);
    if (isReveal == true) {
      icon = Image.asset("assets/images/shijian.png", width: 12, height: 12);
    }

    return Container(
      // color: Colors.red,
      padding: EdgeInsets.only(left: 12, right: 12),
      margin: EdgeInsets.only(bottom: 16, top: 12),
      height: 40,
      width: double.infinity,
      child: Row(
        children: [
          SizedBox(
            width: 62.5,
            child: Text(title, style: TextStyle(fontSize: 13)),
          ),
          // Text(title, style: TextStyle(fontSize: 13)),
          SizedBox(width: 8),
          Expanded(
            //CustomColors.greyColo
            child: Row(
              children: [
                Expanded(
                  child: Container(
                    // width: ScreenTool.screenWidth-24,
                    height: 40,
                    padding: EdgeInsets.only(left: 12, right: 12),
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                      // color: Colors.amber,
                      border: Border.all(
                        width: 1,
                        color: CustomColors.greyColor,
                      ),
                      // color: CustomColors.textColor,
                      borderRadius: BorderRadius.all(Radius.circular(4)),
                    ),

                    child: Stack(
                      children: [
                        subView,
                        Positioned(
                          right: 0,

                          // top: 9
                          top: 0,
                          bottom: 0,
                          child: Center(child: icon),
                        ),
                      ],
                    ),
                  ),
                ),
                // Image.asset("assets/images/shijian.png", width: 12, height: 12),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget _textFieldItem(String title, String hintText, {String unit = ""}) {
    return _basicsItem(
      unit: unit,
      title,

      TextField(
        maxLines: 1,
        // textAlign: TextAlign.center,
        decoration: InputDecoration(
          fillColor: Colors.amber,
          isDense: true, // 添加这一行使内容更紧凑
          border: InputBorder.none,
          // contentPadding: EdgeInsets.symmetric(vertical: 12), // 调整垂直内边距
          focusedBorder: buildInputBorder(),
          enabledBorder: buildInputBorder(),
          disabledBorder: buildInputBorder(),
          errorBorder: buildInputBorder(),
          hintText: hintText,
          contentPadding: EdgeInsets.symmetric(vertical: 0, horizontal: 0),
          // contentPadding: EdgeInsets.symmetric(vertical: 5),
          // alignLabelWithHint: true, // 确保提示文本和输入文本对齐方式一致
          hintStyle: TextStyle(
            color: CustomColors.grey,
            height: 1.0, // 设置行高为1.0
          ),
        ),
      ),
    );
  }
}
